<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        h3 {
            margin-top: 100px;
            text-align: center;
        }
        .box {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,.3);
            display: none;
        }
        .login {
            background-color: #fff;
            width: 500px;
            height: 600px;
            overflow: hidden;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
            cursor: move;

        }
        .login h2 {
            margin-top: 50px;
            text-align: center;
        }
        .login form {
            width: 500px;
            margin-top:50px;
            text-align: center
            
        }
        .login form input{
            width: 250px;
            height: 30px;
        }
        .login form p {
            margin-bottom: 50px;
            text-align: center
        }
        .box a{
            position: absolute;
            right: 5px;
            top: 0;
            text-decoration: none;
            color: #000;
            width: 16px;
            height: 16px;
        }
    </style>
</head>
<body>
    <h3><a href="javascript:void(0)" id='lo'>登录</a></h3>
    <div class="box">
        <div class="login">
            <a href="javascript:void(0)">x</a>
            <h2>登录窗口</h2>
            <form action="#">
                <p>账号:&nbsp;&nbsp;&nbsp;<input type="text" placeholder="请输入账号"></p> 
                <p>密码:&nbsp;&nbsp;&nbsp;<input type="password" placeholder="请输入密码"></p>   
                <button>登录</button>
            </form>
        </div>
    </div>
    <script>
        //获取元素
        var login = document.getElementsByClassName('login')[0];
        var lo = document.getElementById('lo');
        var box = login.parentNode;
        var close = login.children[0];
        //点击登录按钮,登录界面弹出
        lo.onclick = function() {
            box.style.display = 'block';
        }
        close.onclick = function() {
            box.style.display = 'none'
        }
        //按住啊拖动登录框,弹起不拖动
        login.onmousedown = function(e) {
            e = e || window.event
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            document.onmousemove = function(e) {
                login.style.left = e.pageX - x +'px'
                login.style.top = e.pageY - y +'px'
                if(login.offsetLeft <= login.offsetWidth/2) {
                    login.style.left = login.offsetWidth/2 + 'px'
                }
                if(login.offsetTop <= login.offsetHeight/2) {
                    login.style.top = login.offsetHeight/2 + 'px'
                }
            }
            document.onmouseup = function() {
                document.onmousemove = null;
            }
        }
        
    </script>
</body>
</html>